package com.cetcs.kmga.dataView.service.impl;

import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataView.dao.mybatis.CloudDataViewMapper;
import com.cetcs.kmga.dataView.entity.cloudDataView.*;
import com.cetcs.kmga.dataView.entity.cloudDataView.vo.*;
import com.cetcs.kmga.dataView.service.ICloudDataViewService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/1/24 0024.
 */
@Service
public class CloudDataViewServiceImpl implements ICloudDataViewService {

    @Autowired
    private CloudDataViewMapper cloudDataViewMapper;

    @Override
    public List<CloudDataSystemsVo> viewCloudDataSystems(Data params) throws Exception {

        List<CloudDataSystems> list = cloudDataViewMapper.viewCloudDataSystems();
        List<CloudDataSystemsVo> result = Lists.newArrayList();
        for (CloudDataSystems CloudDataSystems : list ){
            CloudDataSystemsVo vo = new CloudDataSystemsVo();
            vo.setId(CloudDataSystems.getAppId());
            vo.setName(CloudDataSystems.getAppName());
            if ("网博WIFI".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc1.png");
            }
            if ("锐安网综".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc2.png");
            }
            if ("美亚舆情".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc3.png");
            }
            if ("美亚网综".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc4.png");
            }
            if ("烽火网综".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc5.png");
            }
            if ("邦富舆情".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc6.png");
            }
            if ("网博网吧".equals(CloudDataSystems.getAppName())){
                vo.setImage("./assets/imgs/zzsc7.png");
            }
            vo.setServer("");
            vo.setStorage("");
            vo.setNetwork("");
            vo.setTime("");
            vo.setAvailability("");
            vo.setIn(0);
            vo.setOut(0);

            result.add(vo);
        }
        //获取最后一个系统的信息
        params.put("id",list.get(list.size()-1).getAppId());
        CloudDataSystemsInfo cloudDataSystemsInfo = cloudDataViewMapper.viewCloudDataSystemsInfo(params);
        CloudDataSystemsVo last = result.get(list.size()-1);
        last.setServer(cloudDataSystemsInfo.getServer());
        last.setStorage(cloudDataSystemsInfo.getStorage());
        last.setNetwork(cloudDataSystemsInfo.getNetwork());
        last.setTime(cloudDataSystemsInfo.getTime());
        last.setAvailability(cloudDataSystemsInfo.getAvailability());

        return result;
    }

    @Override
    public CloudDataSystemsVo viewCloudDataSystemsInfo(Data params) throws Exception {

        CloudDataSystemsInfo cloudDataSystemsInfo = cloudDataViewMapper.viewCloudDataSystemsInfo(params);
        CloudDataSystemsVo result = new CloudDataSystemsVo();

        result.setId(params.getString("id"));
        result.setName(cloudDataSystemsInfo.getName());
        if ("网博WIFI".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc1.png");
        }
        if ("锐安网综".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc2.png");
        }
        if ("美亚舆情".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc3.png");
        }
        if ("美亚网综".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc4.png");
        }
        if ("烽火网综".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc5.png");
        }
        if ("邦富舆情".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc6.png");
        }
        if ("网博网吧".equals(cloudDataSystemsInfo.getName())){
            result.setImage("./assets/imgs/zzsc7.png");
        }

        result.setServer(cloudDataSystemsInfo.getServer());
        result.setStorage(cloudDataSystemsInfo.getStorage());
        result.setNetwork(cloudDataSystemsInfo.getNetwork());
        result.setTime(cloudDataSystemsInfo.getTime());
        result.setAvailability(cloudDataSystemsInfo.getAvailability());
        result.setIn(0);
        result.setOut(0);

        return result;
    }

    @Override
    public List<CloudDataSourcesVo> viewCloudDataSources(Data params) throws Exception {

        CloudDataSources cloudDataSources = cloudDataViewMapper.viewCloudDataSources();
        List<CloudDataSourcesVo> result = Lists.newArrayList();
        CloudDataSourcesVo network = new CloudDataSourcesVo();
        network.setName("网络设备");
        network.setNumber(cloudDataSources.getNetworkNum());
        result.add(network);
        CloudDataSourcesVo server = new CloudDataSourcesVo();
        server.setName("服务器");
        server.setNumber(cloudDataSources.getServerNum());
        result.add(server);
        CloudDataSourcesVo storage = new CloudDataSourcesVo();
        storage.setName("存储设备");
        storage.setNumber(cloudDataSources.getStorageNum());
        result.add(storage);

        return result;
    }

    @Override
    public CloudDataWeatherVo viewCloudDataWeather(Data params) throws Exception {

        CloudDataWeatherAndWorkLoad CloudDataWeatherAndWorkLoad = cloudDataViewMapper.viewCloudDataWeatherandWorkLoad();
        CloudDataWeatherVo result = new CloudDataWeatherVo();

        //总和
        int all = CloudDataWeatherAndWorkLoad.getHealthNum()+CloudDataWeatherAndWorkLoad.getAlarmNum()+CloudDataWeatherAndWorkLoad.getMalNum();
        int normal = (int) Math.round((double)CloudDataWeatherAndWorkLoad.getHealthNum()/all*100);
        int alarm =(int) Math.round((double)CloudDataWeatherAndWorkLoad.getAlarmNum()/all*100);
        int fault = 100-normal-alarm;
        result.setHealth(CloudDataWeatherAndWorkLoad.getScore());
        result.setNormal(String.valueOf(normal));
        result.setAlarm(String.valueOf(alarm));
        result.setFault(String.valueOf(fault));

        return result;
    }

    @Override
    public List<CloudDataSituationalVo> viewCloudDataSituational(Data params) throws Exception {

        CloudDataSituational cloudDataSituational = cloudDataViewMapper.viewCloudDataSituational();
        List<CloudDataSituationalVo> result = Lists.newArrayList();
        CloudDataSituationalVo situation = new CloudDataSituationalVo();
        situation.setName("综合指数");

        if (cloudDataSituational.getSituation().contains(".00")){
            situation.setNumber(cloudDataSituational.getSituation().split("\\.00")[0]);
        }else{
            situation.setNumber(cloudDataSituational.getSituation());
        }
        situation.setType("0");
        result.add(situation);
        CloudDataSituationalVo vulnerable = new CloudDataSituationalVo();
        vulnerable.setName("脆弱性指数");
        if (cloudDataSituational.getVulnerable().contains(".00")){
            vulnerable.setNumber(cloudDataSituational.getVulnerable().split("\\.00")[0]);
        }else{
            vulnerable.setNumber(cloudDataSituational.getVulnerable());
        }
        vulnerable.setType("1");
        result.add(vulnerable);
        CloudDataSituationalVo treat = new CloudDataSituationalVo();
        treat.setName("威胁指数");
        if (cloudDataSituational.getTreat().contains(".00")){
            treat.setNumber(cloudDataSituational.getTreat().split("\\.00")[0]);
        }else{
            treat.setNumber(cloudDataSituational.getTreat());
        }
        treat.setType("2");
        result.add(treat);

        return result;
    }

    @Override
    public CloudDataWorkLoadVo viewCloudDataWorkLoad(Data params) throws Exception {

        CloudDataWeatherAndWorkLoad CloudDataWeatherAndWorkLoad = cloudDataViewMapper.viewCloudDataWeatherandWorkLoad();
        CloudDataWorkLoadVo result = new CloudDataWorkLoadVo();
        List<String> list = Lists.newArrayList();
        list.add(String.valueOf(CloudDataWeatherAndWorkLoad.getMemoryRate()));
        list.add(String.valueOf(CloudDataWeatherAndWorkLoad.getCpuRate()));
        list.add(String.valueOf(CloudDataWeatherAndWorkLoad.getNetRate()));
        list.add(String.valueOf(CloudDataWeatherAndWorkLoad.getDiskRate()));
        result.setValue(list);

        return result;
    }

    @Override
    public DataInOutNum viewDataInOutNum(Data params) throws Exception {
        return cloudDataViewMapper.viewDataInOutNum(params);
    }
}
